package models;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.data.validation.Password;
import play.data.validation.Required;
import play.db.jpa.Model;
import play.libs.Codec;

/**
 * User模块::可以任意添加其他功能，也可以另外添加Model关联User
 * 
 * @see BaseModule
 * @since 1.0
 * @author Mr.J.
 * **/
@Entity
public class User extends Model implements BaseModule{
	@Required(message="name is must be or wrong")
	public String name;
	
	@Required(message="password is must be or wrong")
	@Password
	public String password;
	
	@OneToOne(mappedBy="user",cascade=CascadeType.REMOVE)
	public Permission permission;
	
	@OneToMany(mappedBy="user",cascade=CascadeType.REMOVE)
	List<OperationLog> oplogs;
	
	@OneToMany(mappedBy="user",cascade=CascadeType.REMOVE)
	List<Article> articles;
	
	@ManyToMany(cascade=CascadeType.PERSIST)
	public List<Actions> actions;

	public User(String name, String password) {
		super();
		this.name = name;
		//MD5加密，验证的时候也需要同样的方法先加密再对比
		this.password = Codec.hexMD5(password);
	}
}
